package com.noocodecommit.wicket.stuff.jquery.plugin.ui.spinner;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.form.TextField;

import com.noocodecommit.wicket.stuff.jquery.JQCore;
import com.noocodecommit.wicket.stuff.jquery.JQOptions;
import com.noocodecommit.wicket.stuff.jquery.exception.CannotApplyJQOptionException;
import com.noocodecommit.wicket.stuff.jquery.plugin.IJQPluginComponent;
import com.noocodecommit.wicket.stuff.jquery.plugin.JQPlugin;
import com.noocodecommit.wicket.stuff.jquery.plugin.ui.progressbar.JQProgressBarOptions;

/**
 * Component used to design spinners<br>
 * Jquery use for this behavior is <code>$('#id').spinner(options);</code><br>
 * With Wicket, you just need to create a JQSpinner Component
 * 
 * @author Benoit Bouchez
 * 
 */
public class JQSpinner extends TextField implements IJQPluginComponent
{
	/** JQuery plugin */
	private JQSpinnerPlugin plugin;
	/** JQuery default options */
	private JQSpinnerOptions options = new JQSpinnerOptions();

	/**
	 * Constructor
	 * 
	 * @param id
	 *            wicket component id
	 * @param key
	 *            plugin unique id on which component is attached
	 */
	public JQSpinner(String id, String key)
	{
		super(id);
		this.setOutputMarkupId(true);
		if (key == null)
		{
			key = JQSpinnerPlugin.DEFAULT_KEY;
		}
		if (JQCore.get().getPlugin(key) == null)
		{
			new JQSpinnerPlugin(key);
		}
		this.plugin = (JQSpinnerPlugin) JQCore.get().getPlugin(key);
		JQCore.get().getPlugin(key).attachComponentForDocumentReady(this);
	}

	/**
	 * Constructor
	 * 
	 * @param id
	 *            wicket component id
	 */
	public JQSpinner(String id)
	{
		this(id, null);
	}

	public JQSpinnerOptions getOptions()
	{
		return options;
	}

	public void setOptions(JQOptions options)
	{
		if (options.getClass().equals(JQProgressBarOptions.class))
		{
			this.options = (JQSpinnerOptions) options;
		}
		else
		{
			throw new CannotApplyJQOptionException(JQSpinner.class, options.getClass(), JQSpinnerOptions.class);
		}
	}

	public JQSpinnerPlugin getPlugin()
	{
		return plugin;
	}

	public void setPlugin(JQPlugin plugin)
	{
		this.plugin = (JQSpinnerPlugin) plugin;
	}

	public String getWidgetReadyScript(boolean reset)
	{
		return "$('#" + this.getMarkupId() + "')." + (reset ? "spinner('destroy')." : "") + options.getDocumentReadyOptions();
	}

	public String getAfterAjaxUpdateScript()
	{
		return getWidgetReadyScript(true);
	}

	/**
	 * Enables this spinner<br>
	 * Appends a javascript call <code>$('#id').spinner('enable')</code> to the ajax target.
	 * 
	 * @param target
	 *            Ajax target
	 */
	public void enable(AjaxRequestTarget target)
	{
		target.appendJavascript("$('#" + this.getMarkupId() + "').spinner('enable')");
	}
	
	/**
	 * Disable this spinner<br>
	 * Appends a javascript call <code>$('#id').spinner('disable')</code> to the ajax target.
	 * 
	 * @param target
	 *            Ajax target
	 */
	public void disable(AjaxRequestTarget target)
	{
		target.appendJavascript("$('#" + this.getMarkupId() + "').spinner('disable')");
	}
	
	/**
	 * Destroy this spinner<br>
	 * Appends a javascript call <code>$('#id').spinner('destroy')</code> to the ajax target.
	 * 
	 * @param target
	 *            Ajax target
	 */
	public void destroy(AjaxRequestTarget target)
	{
		target.appendJavascript("$('#" + this.getMarkupId() + "').spinner('destroy')");
	}

}
